class CAgent:
    def __init__(self):
        self.class_name = 'bt_'
        self.bt = None

    def init_bt(self, env, sideName, lenAI, options):
        side = env.scenario.get_side_by_name(sideName)

        sideGuid = side.strGuid
        shortSideKey = "a" + str(lenAI + 1)
        attributes = options
        # 行为树的节点
        hxSequence = BT() #总的根节点
        hxSelector = BT() #进攻判断和防守选择节点

        # 巡逻节点，左树部分
        hxSequence_D = BT() # 防守根节点，左
        missionSelector_D = BT()
        missionSelectorCondition = BT() # 防守任务选择条件
        patrolMissionSelector_D = BT()
        updatePatrolMission_D = BT()
        createPatrolMission_D = BT()

        # 反舰节点，子树右树部分
        AntiSurfaceShipMissionSelector = BT()
        CreateAntiSurfaceShipMission = BT()
        UpdateAntiSurfaceShipMission = BT()

        #条令设置节点
        SetDoctrine = BT()
        Support = BT()
        StrikeToPatrol =BT()
        AttackerCondition = BT()

        #进攻树节点
        hxSequence_A = BT()  # 进攻根节点，右
        missionSelector_A = BT()
        patrolMissionSelector_A = BT()
        createPatrolMission_A = BT()
        updatePatrolMission_A = BT()

        # 连接节点形成树
        hxSequence.add_child(hxSelector)
        hxSelector.add_child(AttackerCondition)
        hxSelector.add_child(hxSequence_D)
        '''---------------------------------------------------------------------------'''
        hxSequence_D.add_child(SetDoctrine) #条令
        hxSequence_D.add_child(Support)     # 开船
        hxSequence_D.add_child(missionSelector_D)
        hxSequence_D.add_child(AntiSurfaceShipMissionSelector)
        missionSelector_D.add_child(missionSelectorCondition)
        missionSelector_D.add_child(patrolMissionSelector_D)

        patrolMissionSelector_D.add_child(updatePatrolMission_D)
        patrolMissionSelector_D.add_child(createPatrolMission_D)
        patrolMissionSelector_D.add_child(StrikeToPatrol)

        hxSequence.add_child(hxSequence_A)
        hxSequence_A.add_child(SetDoctrine)  # 条令
        hxSequence_A.add_child(Support)  # 开船
        hxSequence_A.add_child(missionSelector_A)
        hxSequence_A.add_child(AntiSurfaceShipMissionSelector)
        missionSelector_A.add_child(missionSelectorCondition)
        missionSelector_A.add_child(patrolMissionSelector_A)

        patrolMissionSelector_A.add_child(updatePatrolMission_A)
        patrolMissionSelector_A.add_child(createPatrolMission_A)
        patrolMissionSelector_A.add_child(StrikeToPatrol)

        AntiSurfaceShipMissionSelector.add_child(CreateAntiSurfaceShipMission)
        AntiSurfaceShipMissionSelector.add_child(UpdateAntiSurfaceShipMission)
        AntiSurfaceShipMissionSelector.add_child(StrikeToPatrol)
        '''------------------------------------------------------'''
        # 每个节点执行的动作
        hxSequence.set_action(hxSequence.sequence, sideGuid, shortSideKey, attributes)
        hxSelector.set_action(hxSelector.select, sideGuid, shortSideKey, attributes)
        AttackerCondition.set_action(contact_analysis,sideGuid, shortSideKey, attributes)

        '''----------------------------------------------'''
        hxSequence_D.set_action(hxSequence_D.sequence, sideGuid, shortSideKey, attributes)
        SetDoctrine.set_action(set_side_doctrine, sideGuid, shortSideKey, attributes) # 条令
        Support.set_action(create_support_mission,sideGuid, shortSideKey, attributes) # 开船
        missionSelector_D.set_action(missionSelector_D.select, sideGuid, shortSideKey, attributes)
        missionSelectorCondition.set_action(antiship_condition_check, sideGuid, shortSideKey, attributes)

        patrolMissionSelector_D.set_action(patrolMissionSelector_D.select, sideGuid, shortSideKey, attributes)

        updatePatrolMission_D.set_action(update_patrol_mission, sideGuid, shortSideKey, attributes)
        createPatrolMission_D.set_action(create_patrol_mission, sideGuid, shortSideKey, attributes)

        hxSequence_A.set_action(hxSequence_A.sequence, sideGuid, shortSideKey, attributes)
        missionSelector_A.set_action(missionSelector_A.select, sideGuid, shortSideKey, attributes)
        patrolMissionSelector_A.set_action(patrolMissionSelector_A.select, sideGuid, shortSideKey, attributes)
        updatePatrolMission_A.set_action(update_defensive_patrol, sideGuid, shortSideKey, attributes)
        createPatrolMission_A.set_action(create_defensive_patrol, sideGuid, shortSideKey, attributes)
        AntiSurfaceShipMissionSelector.set_action(AntiSurfaceShipMissionSelector.select, sideGuid, shortSideKey, attributes)
        CreateAntiSurfaceShipMission.set_action(create_antisurfaceship_mission, sideGuid, shortSideKey, attributes)
        UpdateAntiSurfaceShipMission.set_action(update_antisurfaceship_mission, sideGuid, shortSideKey, attributes)
        StrikeToPatrol.set_action(antiship_to_patrol,sideGuid,shortSideKey,attributes)
        '''----------------------------------'''
        self.bt = hxSequence

    # 更新行为树
    def update_bt(self, side_name, scenario):
        return self.bt.run(side_name, scenario)